// pages/find/find.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag: true,
    banners: [],
    poster: "http://47.104.209.44/base/imgs/1.jpg",
    name: "我是大冶人",
    author: "小垃圾",
    src: "https://nx01-sycdn.kuwo.cn/056b32fa2732b6338b565989ff881a7a/61c1a57a/resource/n3/98/78/33178199.mp3",
    vSrc: "https://vd4.bdstatic.com/mda-kemzmyegwkwss8di/hd/mda-kemzmyegwkwss8di.mp4?v_from_s=hkapp-haokan-hbe&auth_key=1640083153-0-0-da8ba18bc621024b6d75ef0b13cc68ab&bcevod_channel=searchbox_feed&pd=1&pt=3&abtest=3000202_1&klogid=0553176360",
    danmuList: [{
      text: '第 1s 出现的弹幕',
      color: '#ff0000',
      time: 1
    }, {
      text: '第 3s 出现的弹幕',
      color: '#ff00ff',
      time: 3
    }],
    danmu: "666",
    toggle:true

  },
  videotimeupdate() {
    if (!wx.getStorageSync('isplay')&& this.data.toggle) {
      this.showDanger()
      this.setData({
        toggle:false
      })
    }
  },
  videoplay() {
    if (!wx.getStorageSync('isplay')) {
      this.showDanger()
    }
  },
  showDanger() {
    this.videoCtx.pause()
    wx.showModal({
      title: "流量警告",
      content: "你正在使用",
      cancelText: "取消观看",
      confirmText: "继续观看",
      cancelColor: "#000",
      confirmColor: "#f50",
      success: res => {
        if (res.confirm) {
          this.videoCtx.play()
          wx.setStorageSync('isplay', true)
        } else if (res.cancel) {
          wx.showTabBar({
            title: "你不想观看",
            icon: "none"
          })
        }
      }
    })
  },
  changeVidepoSrc() {
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: ['front', 'back'],
      success: (res) => {
        this.setData({
          vSrc: res.tempFilePath
        })
      }
    })
  },
  sendDanmu() {
    this.videoCtx.sendDanmu({
      text: this.data.danmu,
      // color:getRandomColor()
    })
  },

  clicktoplay() {
    this.audioCtx.pause()
  },
  clkicktopause() {
    this.audioCtx.play()
  },

  timeendfunc() {
    wx.showToast({
      title: "加载成功",
      icon: "none",
      duration: 1200

    })
    this.setData({
      flag: false
    })
  },

  getBanner() {
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: 'http://47.104.209.44:3333/banner',
      data: {

      },
      header: {

      },
      success: res => {
        console.log(res);
        this.setData({
          banners: res.data.banners
        })
        wx.hideLoading()

      },
      fail: err => {

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBanner()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.audioCtx = wx.createAudioContext("myaudio")
    this.videoCtx = wx.createVideoContext('myVideo')

    // 获取网络状态
    wx.getNetworkType({
      success(res) {
        const networkType = res.networkType
        console.log(networkType);
        wx.setStorageSync('isplay', networkType == "wifi")
      }
    })
    // 监听网络状态改变
    wx.onNetworkStatusChange(function (res) {
      console.log(res.isConnected)
      console.log(res.networkType)
      wx.setStorageSync('isplay', res.networkType == "wifi")
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})